package swings.others;

import swings.panels.WorkPanel;
import utils.CommonUtils;

import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.JTableHeader;
import java.awt.*;

/**
 * @version V1.0
 * @Title:
 * @Package
 * @Description: 通用table，重写了构造方法
 * @author: GaleBlade
 * @date:
 */
public class CommonTable extends JTable {

    /**
     * 构造方法
     */
    public CommonTable(Object[][] tableData, Object[] columnTitle) {
        super(tableData, columnTitle);
        //设置字体
        this.setFont(CommonUtils.getFont());
        //设置行高
        this.setRowHeight(30);
        //设置table大小
        this.setPreferredScrollableViewportSize(new Dimension(WorkPanel.width-20,WorkPanel.height/2-40));
        this.setOpaque(false);
        this.setBorder(BorderFactory.createLineBorder(Color.WHITE));
        this.setForeground(Color.WHITE);
        this.setGridColor(Color.WHITE);
        this.setSelectionBackground(Color.BLUE);
        //render单元格的属性
        DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
        renderer.setOpaque(false);
        //设置表头
        JTableHeader tableHeader = this.getTableHeader();
        //设置表头高度
        tableHeader.setPreferredSize(new Dimension(tableHeader.getWidth(), 30));
        tableHeader.setFont(CommonUtils.getFont());
//        tableHeader.setOpaque(false);
        tableHeader.setForeground(Color.WHITE);
        tableHeader.setBackground(new Color(24, 144, 255));
        tableHeader.setBorder(BorderFactory.createLineBorder(Color.WHITE, 1, true));
        tableHeader.setVisible(false);

        //遍历表格中所有列，将其渲染器设置为renderer
        for (int i=0;i<columnTitle.length;i++) {
            this.getColumn(columnTitle[i]).setCellRenderer(renderer);
            tableHeader.getColumnModel().getColumn(i).setHeaderRenderer(renderer);
        }
    }

    /**
     * 设置单元格可编辑
     */
    public boolean isCellEditable(int row, int column) {return true;}

}
